//
//  SRPersonalDataView.m
//  SportRing
//
//  Created by icelee on 14-11-1.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRPersonalDataView.h"
#import "SRModifyPersonalBirthdayView.h"
#import "SRModifyPersonalHeaderView.h"
#import "SRModifyPersonalHeightView.h"
#import "SRModifyPersonalNicknameView.h"
#import "SRModifyPersonalSexView.h"
#import "SRModifyPersonalWeightView.h"

@implementation SRPersonalDataViewModel


@end


@interface SRPersonalDataView()<UITableViewDataSource,UITableViewDelegate,SRMofiyPersonalBaseViewDelegate,SRModifyPersonalHeaderViewDelegate>

@property (nonatomic, weak)IBOutlet UITableView *srPersonalDataTableView;

@property (nonatomic, strong)NSString *headerImage;
@property (nonatomic, strong)NSString *birthDay;
@property (nonatomic, strong)NSString *weight;
@property (nonatomic, strong)NSString *height;
@property (nonatomic, strong)NSString *nickname;
@property (nonatomic, strong)NSString *sex;

@end

@implementation SRPersonalDataView


- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self.srPersonalDataTableView setBackgroundColor:[UIColor colorWithRed:246.0/255.0 green:245.0/255.0 blue:240.0/255.0 alpha:1.0]];
}

- (SR_SEX_TYPE)sex:(NSString *)sexStr
{
    if ([sexStr isEqualToString:@"男"])
    {
        return SR_SEX_TYPE_MALE;
    }
    if ([sexStr isEqualToString:@"女"])
    {
        return SR_SEX_TYPE_FAMALE;
    }
    
    return SR_SEX_TYPE_UNKNOWN;
    
}

- (void)setSRPersonalDataViewData:(SRPersonalDataViewModel *)model
{
    self.headerImage = model.headerImage;
    self.birthDay = model.birthDay; //formattedBy_yyyyMd_CHN
    self.weight = [NSString stringWithFormat:@"%ldkg",(long)model.weight];
    self.height = [NSString stringWithFormat:@"%ldcm",(long)model.height];
    self.nickname = model.nickname;
    if (model.sexType == SR_SEX_TYPE_FAMALE)
    {
         self.sex = @"女";
    }
    if (model.sexType == SR_SEX_TYPE_MALE)
    {
        self.sex = @"男";
    }
    
    [self.srPersonalDataTableView reloadData];
    
}

- (void)setSRPersonalDataViewHeader:(NSString *)imageUrl
{
    self.headerImage = imageUrl;
    
    [self.srPersonalDataTableView reloadData];
    
}
- (void)setSRPersonalDataViewNickName:(NSString *)nickName
{
    self.nickname = nickName;
    
    [self.srPersonalDataTableView reloadData];
    
}
- (void)setSRPersonalDataViewSex:(SR_SEX_TYPE )sexType
{
    if (sexType == SR_SEX_TYPE_FAMALE)
    {
        self.sex = @"女";
    }
    if (sexType == SR_SEX_TYPE_MALE)
    {
        self.sex = @"男";
    }
    [self.srPersonalDataTableView reloadData];
    
}
- (void)setSRPersonalDataViewBitthDay:(NSDate *)birthDay
{
    self.birthDay = [birthDay formattedBy_yyyyMd_CHN];
    [self.srPersonalDataTableView reloadData];
    
}
- (void)setSRPersonalDataViewWeight:(NSInteger)weight
{
    self.weight = [NSString stringWithFormat:@"%ldkg",(long)weight];
    [self.srPersonalDataTableView reloadData];
    
}
- (void)setSRPersonalDataViewHeight:(NSInteger)height
{
    self.height = [NSString stringWithFormat:@"%ldcm",(long)height];
    [self.srPersonalDataTableView reloadData];
    
}


#pragma mark - UITableViewDataSource ,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    if (section == 0)
    {
        return 2;
    }
    else
    {
        return 1;
    }
    
    return 0;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{
    UIView *footerView = nil;

    footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srPersonalDataTableView.frame.size.width, 7)];
    footerView.backgroundColor=[UIColor clearColor];
    return footerView;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    UIView *headerView = nil;
    
    if (section == 0)
    {
        
        headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srPersonalDataTableView.frame.size.width, 7)];
        headerView.backgroundColor=[UIColor clearColor];
        return headerView;

    }
    else
    {
        return nil;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 7;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 48;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SRPersonalDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SRPersonalDataCell"];
    
    //TO DO: set data.
    SRPersonalDataCellModel *personDataModel = nil;
    
    if (indexPath.section == 0)
    {
        
        if (indexPath.row == 0)
        {
            personDataModel = [[SRPersonalDataCellModel alloc] initWithTitle:@"头像" content:@"" headerImage:_headerImage];
            
        }
        if (indexPath.row == 1)
        {
            personDataModel = [[SRPersonalDataCellModel alloc] initWithTitle:@"昵称" content:_nickname headerImage:nil];
            
        }
    }
    
    if (indexPath.section == 1)
    {
        personDataModel = [[SRPersonalDataCellModel alloc] initWithTitle:@"性别" content:_sex headerImage:nil];
    }
    if (indexPath.section == 2)
    {
        personDataModel = [[SRPersonalDataCellModel alloc] initWithTitle:@"生日" content:_birthDay headerImage:nil];
    }
    if (indexPath.section == 3)
    {
        personDataModel = [[SRPersonalDataCellModel alloc] initWithTitle:@"体重" content:_weight headerImage:nil];
    }
    if (indexPath.section == 4)
    {
        personDataModel = [[SRPersonalDataCellModel alloc] initWithTitle:@"身高" content:_height headerImage:nil];
    }
    
    [cell setSRPersonalDataCellData:personDataModel];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        //头像
        if (indexPath.row == 0)
        {
            SRModifyPersonalHeaderView *modifyView = [SRModifyPersonalHeaderView LoadSRModifyPersonalHeaderView];
            modifyView.mpDelegate = self;
            modifyView.delegate = self;
            [modifyView showAtBottom];
            
        }
        
        //昵称
        if (indexPath.row == 1)
        {
            SRModifyPersonalNicknameView *modifyView = [SRModifyPersonalNicknameView LoadSRModifyPersonalNicknameView];
            modifyView.nicknameTextField.text = self.nickname;
            modifyView.delegate = self;
            [modifyView showAtBottom];
            
        }
        
    }
    if (indexPath.section == 1)
    {
        //性别
        if (indexPath.row == 0)
        {
            SRModifyPersonalSexView *modifyView = [SRModifyPersonalSexView LoadSRModifyPersonalSexView];
            modifyView.sexType = [self sex:self.sex];
            modifyView.delegate = self;
            [modifyView showAtBottom];
            
        }
        
    }
    if (indexPath.section == 2)
    {
        //生日
        if (indexPath.row ==0)
        {
            SRModifyPersonalBirthdayView *modifyView = [SRModifyPersonalBirthdayView LoadSRModifyPersonalBirthdayView];
//            modifyView.birthDayDate = self.birthDay;
            modifyView.delegate = self;
            [modifyView showAtBottom];
            
        }
        
    }
    if (indexPath.section == 3)
    {
        //体重
        if (indexPath.row == 0)
        {
            SRModifyPersonalWeightView *modifyView = [SRModifyPersonalWeightView LoadSRModifyPersonalWeightView];
            modifyView.currentWeight = [self.weight integerValue];
            modifyView.delegate = self;
            [modifyView showAtBottom];
            
        }
        
    }
    if (indexPath.section == 4)
    {
        //身高
        if (indexPath.row == 0)
        {
            SRModifyPersonalHeightView *modifyView = [SRModifyPersonalHeightView LoadSRModifyPersonalHeightView];
            modifyView.currentHeight = [self.height integerValue];
            modifyView.delegate = self;
            [modifyView showAtBottom];
            
        }
        
    }
 
}


#pragma mark - SRMofiyPersonalBaseViewDelegate

- (void)didClickedCancelButton:(SRMofiyPersonalBaseView *)stMofiyPersonalBaseView
{

    
}
- (void)didClickedSureButton:(SRMofiyPersonalBaseView *)stMofiyPersonalBaseView
{
    [stMofiyPersonalBaseView fadeOut];
    if ([stMofiyPersonalBaseView isKindOfClass:[SRModifyPersonalHeaderView class]])
    {
        if (_personDataDelegate && [_personDataDelegate respondsToSelector:@selector(didModifyHeader)])
        {
            [_personDataDelegate didModifyHeader];
        }
        
    }
    if ([stMofiyPersonalBaseView isKindOfClass:[SRModifyPersonalNicknameView class]])
    {
        SRModifyPersonalNicknameView *nick = (SRModifyPersonalNicknameView *)stMofiyPersonalBaseView;
        
        if (_personDataDelegate && [_personDataDelegate respondsToSelector:@selector(didModifyNickname:)])
        {
            [self setSRPersonalDataViewNickName:nick.nicknameTextField.text];
            [_personDataDelegate didModifyNickname:nick.nicknameTextField.text];
            
        }
        
    }
    if ([stMofiyPersonalBaseView isKindOfClass:[SRModifyPersonalSexView class]])
    {
        SRModifyPersonalSexView *sex = (SRModifyPersonalSexView *)stMofiyPersonalBaseView;
        
        if (_personDataDelegate && [_personDataDelegate respondsToSelector:@selector(didModifySex:)])
        {
            [self setSRPersonalDataViewSex:sex.sexType];
            [_personDataDelegate didModifySex:sex.sexType];
        }
        
    }
    if ([stMofiyPersonalBaseView isKindOfClass:[SRModifyPersonalBirthdayView class]])
    {
        SRModifyPersonalBirthdayView *birthDay = (SRModifyPersonalBirthdayView *)stMofiyPersonalBaseView;
        
        if (_personDataDelegate && [_personDataDelegate respondsToSelector:@selector(didModifyBirthDay:)])
        {
            [self setSRPersonalDataViewBitthDay:birthDay.birthDayDate];
            [_personDataDelegate didModifyBirthDay:birthDay.birthDayDate];
        }
        
    }
    if ([stMofiyPersonalBaseView isKindOfClass:[SRModifyPersonalWeightView class]])
    {
        SRModifyPersonalWeightView *weight = (SRModifyPersonalWeightView *)stMofiyPersonalBaseView;
        
        if (_personDataDelegate && [_personDataDelegate respondsToSelector:@selector(didModifyWeight:)])
        {
            [self setSRPersonalDataViewWeight:weight.currentWeight];
            [_personDataDelegate didModifyWeight:weight.currentWeight];
        }
        
    }
    if ([stMofiyPersonalBaseView isKindOfClass:[SRModifyPersonalHeightView class]])
    {
        SRModifyPersonalHeightView *height = (SRModifyPersonalHeightView *)stMofiyPersonalBaseView;
        
        if (_personDataDelegate && [_personDataDelegate respondsToSelector:@selector(didModifyHeight:)])
        {
            [self setSRPersonalDataViewHeight:height.currentHeight];
            [_personDataDelegate didModifyHeight:height.currentHeight];
        }
        
    }
}

#pragma mark - SRModifyPersonalHeaderViewDelegate

- (void)gotoPhotoAlbum
{
    if (_personDataDelegate && [_personDataDelegate respondsToSelector:@selector(gotoAlbum)])
    {
        [_personDataDelegate gotoAlbum];
    }
    
}

- (void)takePhoto
{
    if (_personDataDelegate && [_personDataDelegate respondsToSelector:@selector(takePhoto)])
    {
        [_personDataDelegate takePhoto];
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
